Well it turns out to run Ruby you still need to install a Ruby Interpreter, which I did.
Here are some very early examples trying to use TDD:
require'test/unit'require'die'class DieTest <Test::Unit::TestCasedef test_roll
rolledValues = Array.newfor num in(0..6)
rolledValues[num] = 0end10000.timesdo
v = Die.new.roll
rolledValues[v] = rolledValues[v]+1end
assert_equal(0, rolledValues[0])for i in(1..6)
assert(rolledValues[i]>1000, 'missing number: '+ i.to_s)endendend
class Die
def initialize()
roll
enddef roll
@faceValue = rand(6)+1enddef faceValue
@faceValueendend
require'test/unit/testcase'require'test/unit/autorunner'require'pair_of_dice'class PairOfDiceTest <Test::Unit::TestCasedef test_roll_returns_valid
p = PairOfDice.new
v = p.roll
assert(v >=2&& v <= 12)endend
This works, however I want to be able to run all of my tests in one fell swoop. In Eclipse using Java, I simply right-click on a project and select Run->Junit Tests and it's all good. This feature doesn't seem to be available in Test::Unit (the Ruby equivalent) so after several attempts, I think I've got what I'm looking for:
require'test/unit'require'find'def allFiles
files = Array.newFind.find('./'){|f|if(f =~ /_test/)
f = f.sub(/\.\//, "").sub(/\.rb$/,"")
files.push(f)end}
files
end
allFiles().each{|f|require f}
This was adequate until I checked my code into subversion. When I did that, I had the original versions of the files in the .svn directory. I then updated this to prune the .svn directory:
require'test/unit'require'find'def allFiles
files = Array.newFind.find('./'){|f|if(f =~ /\.svn/)Find.pruneif f =~ /\.svn/endif(f =~ /_test/)
f = f.sub(/\.\//, "").sub(/\.rb$/,"")
files.push(f)end}
files
end
allFiles().each{|f|require f}
Of course, on the Ruby Forum, Ryan Davis gave me a MUCH more concise (and frankly I think MUCH better) version of this:
(%w(test/unit)+Dir['**/*_test.rb']).each{|f|require f }
All "require"d files are read in using a single array. To create the array we first create an array using words (in this case it saves nothing to use %w(test/unit) versus ['test/unit']. The + operation on an array adds the contents of one array to another and the Dir class's [] method recursively finds files using a syntax I'm familiar with from ant.
This generates an array that looks like the following:
['test/unit', 'die_test', 'pair_of_dice_test']
Then we require each of theses. By requiring test/unit, all other files read in using require that inherit from Test::Unit::TestCase are automatically added to a suite.
Having managed the first version, I continued with the first release of Monopoly.
Well it turns out to run Ruby you still need to install a Ruby Interpreter, which I did.
Here are some very early examples trying to use TDD:
This works, however I want to be able to run all of my tests in one fell swoop. In Eclipse using Java, I simply right-click on a project and select Run->Junit Tests and it's all good. This feature doesn't seem to be available in Test::Unit (the Ruby equivalent) so after several attempts, I think I've got what I'm looking for:
This was adequate until I checked my code into subversion. When I did that, I had the original versions of the files in the .svn directory. I then updated this to prune the .svn directory:
Of course, on the Ruby Forum, Ryan Davis gave me a MUCH more concise (and frankly I think MUCH better) version of this:
All "require"d files are read in using a single array. To create the array we first create an array using words (in this case it saves nothing to use %w(test/unit) versus ['test/unit']. The + operation on an array adds the contents of one array to another and the Dir class's [] method recursively finds files using a syntax I'm familiar with from ant.
This generates an array that looks like the following:
Then we require each of theses. By requiring test/unit, all other files read in using require that inherit from Test::Unit::TestCase are automatically added to a suite.
Having managed the first version, I continued with the first release of Monopoly.